class tPalette
{
  tPaletteFlavor flavors[];
  // in the form [x,y,w,h]
  int dims[];
  // <0.05 is hidden, >0.05 is floating on the top of the screen
  float state;
  int recessedY;
  int extendedY;
  boolean active;
  
  tPalette()
  {
    int halfx = screenSize[0] / 2;
    int imageWidthHalf = 1000 / 2;
    int imageHeight = 100;
    recessedY = -imageHeight+25;
    extendedY = 0;
    int newDims[] = {halfx-imageWidthHalf, recessedY, imageWidthHalf*2, imageHeight};
    dims = newDims;
    active = false;
    state = 0;
    int numFlavors = 10;
    flavors = new tPaletteFlavor[numFlavors];
    for (int i = 0; i < numFlavors; i++)
    {
      color newColor = color(0,0,0);
      String newName = "";
      switch(i)
      {
        case 0:
          newColor = color(237,238,189);
          newName = "CALCIUM";
          break;
        case 1:
          newColor = color(183,251,150);
          newName = "CARBON";
          break;
        case 2:
          newColor = color(206,244,244);
          newName = "FIRE";
          break;
        case 3:
          newColor = color(219,193,155);
          newName = "HELIUM";
          break;
        case 4:
          newColor = color(231,235,235);
          newName = "HYDROGEN";
          break;
        case 5:
          newColor = color(218,207,232);
          newName = "ICE";
          break;
        case 6:
          newColor = color(197,196,198);
          newName = "SILICON";
          break;
        case 7:
          newColor = color(250,176,71);
          newName = "SODIUM";
          break;
        case 8:
          newColor = color(250,227,71);
          newName = "SULFUR";
          break;
        case 9:
          newColor = color(208,12,7);
          newName = "TITANIUM";
          break;
      }
      int fCoords[][] = {{dims[0]+dims[2]/numFlavors*i, dims[1]},
                         {dims[0]+dims[2]/numFlavors*(i+1), dims[1]},
                         {dims[0]+dims[2]/numFlavors*(i+1), dims[1]+dims[3]},
                         {dims[0]+dims[2]/numFlavors*i, dims[1]+dims[3]}};
      flavors[i] = new tPaletteFlavor(fCoords, newColor, newName, i);
    }
  }
  
  // all of this updates the position of the fullShape
  // and the alphas of the cornerShape and fullShape
  void update(int[] mousePos)
  {
    active = false;
    if (mousePos[0] > dims[0] && mousePos[0] < (dims[0]+dims[2]) &&
        mousePos[1] < (dims[1]+dims[3]) && mousePos[1] > dims[1])
    {
      active = true;
    }
    
    if (active)
    {
      state += 0.05;
      state = min(state, 1.0);
    }
    else
    {
      state -= 0.05;
      state = max(state, 0);
    }
     
    if (state > 0.05 && state < 0.95)
    {
      dims[1] = (int)((float)(extendedY-recessedY) * state) + recessedY;
    }
    else
    {
      if (state <= 0.05)
      {
        dims[1] = recessedY;
      }
      else
      {
        dims[1] = extendedY;
      }
    }
    
    for (int i = 0; i < flavors.length; i++)
    {
      flavors[i].setYPosition(dims[1]);
    }
  }
  
  boolean doDraw()
  {
    image(img.palette, dims[0], dims[1]);
    for (int i = 0; i < flavors.length; i++)
    {
      if (!flavors[i].doDraw())
      {
        return false;
      }
    }
    return true;
  }
  
  /**
   * Gets the name of the element being collided with at point pos
   * @return "" or the name of the collided element
   */
  String getCollisionName(int[] pos)
  {
    for (int i = 0; i < flavors.length; i++)
    {
      if (flavors[i].getCollision(pos))
      {
        return flavors[i].getFlavorName();
      }
    }
    return "";
  }
  
  /**
   * Gets the color of the element being collided with at point pos
   * @return the color or 0 (so don't make any of the colors 0)
   */
  color getCollisionColor(int[] pos)
  {
    for (int i = 0; i < flavors.length; i++)
    {
      if (flavors[i].getCollision(pos))
      {
        return flavors[i].getFlavorColor();
      }
    }
    return 0;
  }
  
  /**
   * Gets the index of the element being collided with at point pos
   * @return the index or -1
   */
  color getCollisionFlavorIndex(int[] pos)
  {
    for (int i = 0; i < flavors.length; i++)
    {
      if (flavors[i].getCollision(pos))
      {
        return flavors[i].getFlavorIndex();
      }
    }
    return -1;
  }
}

class tPaletteFlavor
{
  tTexture myTexture;
  color myColor;
  int coords[][];
  String myName;
  int myFlavorIndex;
  
  tPaletteFlavor(int[][] newCoords, color newColor, String name, int newFlavorIndex)
  {
    coords = newCoords;
    myTexture = null;
    myColor = newColor;
    myName = name;
    myFlavorIndex = newFlavorIndex;
  }
  
  tPaletteFlavor(int[][] newCoords, color newColor, String name, int newFlavorIndex, tTexture newTexture)
  {
    coords = newCoords;
    myTexture = newTexture;
    myColor = newColor;
    myName = name;
    myFlavorIndex = newFlavorIndex;
  }
  
  String getFlavorName()
  {
    return myName;
  }
  
  color getFlavorColor()
  {
    return myColor;
  }
  
  int getFlavorIndex()
  {
    return myFlavorIndex;
  }
  
  /**
   * Checks for a collision between the point pos and this flavor
   * @pos the coordinates of the point to test for
   * @return true if there is a collision, false otherwise
   */
  boolean getCollision(int[] pos)
  {
    int coordsA[][] = {pos};
    int posA[] = {0,0};
    int coordsB[][] = coords;
    int posB[] = {0,0};
    
    return checkCollision(coordsA, coordsB, posA, posB);
  }
  
  /**
   * sets the y position of the flavor
   */
  void setYPosition(int pos)
  {
    int h = coords[2][1] - coords[0][1];
    coords[0][1] = pos;
    coords[1][1] = pos;
    coords[2][1] = h + pos;
    coords[3][1] = h + pos;
  }
  
  /**
   * Draws the flavor
   * @return returns the result of the texture's draw
   */
  boolean doDraw()
  {
    int pos[] = {0,0};
    if (myTexture != null)
    {
      return myTexture.doDraw(coords, pos);
    }
    
    return true;
  }
}
